// pages/confirmorder/confirmorder.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    agreement: false,
    isBalance: false,
    info: {
      meal_id: '',
      product: {
        id: '',
        price: '',
        name: '',
        main_img: '',
        time: '',
        unit: '',
        cat_id: ''
      },
      address: {
        id: '',
        name: '',
        phone: '',
        address: '',
        province_id: '',
        city_id: '',
        district_id: ''
      },
      time: {
        start_at: '',
        end_at: '',
        hours: 1
      },
      cleaner: {
        id: '',
        wx_avatar_url: '',
        real_name: ''
      },
      coupon: {
        id: '',
        price: '',
      },
      remark: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    if(options.productID) {
      that.setData({
        'info.product.id': options.productID,
      })
    } 

    if (options.meal_id) {
      that.setData({
        'info.meal_id': options.meal_id
      })
    }

    if (options.hours) {
      that.setData({
        'info.time.hours': options.hours
      })
    } 
    
    if(options.info) {
      that.setData({
        info: JSON.parse(options.info)
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initApp()
  },

  /**
   * initApp
   */
  initApp: function() {
    let that = this,
        product
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/product/detail?access_token=' + accessToken,
        data: {
          id: that.data.info.product.id
        },
        success: res => {
          product = res.data.data.detail
          if (res.data.status === 1) {
            that.setData({
              'info.product.id': product.id,
              'info.product.main_img': product.main_img,
              'info.product.name': product.name,
              'info.product.price': product.price,
              'info.product.time': product.time,
              'info.product.unit': product.unit,
              'info.product.cat_id': product.service_category_id
            })
            that.getDefaultAddr()
          }
        }
      })
    })
  },

  /**
   * 获取默认地址
   */
  getDefaultAddr: function() {
    let that = this
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/addr/default?access_token=' + accessToken,
        success: res => {
          if(res.data.status === 1) {
            if (that.data.info.address.id == '') {
              that.setData({
                'info.address': res.data.data.default
              })
            }
            wx.hideLoading()
          }
        }
      })
    })
  },

  /**
   * 选择服务时间
   */
  time: function() {
   wx.navigateTo({
     url: './chosentime/chosentime?info=' + JSON.stringify(this.data.info),
   })
  },

  /**
   * 选择地址
   */
  address: function() {
    wx.navigateTo({
      url: '../user/address/address?info=' + JSON.stringify(this.data.info),
    })
  },

  /**
   * 选择保洁
   */
  clean: function() {
    if(this.data.info.time.start_at == '') {
      wx.showToast({
        icon: 'none',
        title: '请选择服务时间',
      })
      return false
    }
    wx.navigateTo({
      url: '../cleaner/cleaner?info=' + JSON.stringify(this.data.info)
    })
  },

  /**
   * 
   */
  toPay: function() {
    let that = this,
        myApp = that.data.info
    if(myApp.address.id == '') {
      wx.showToast({
        icon: 'none',
        title: '请选择服务地址',
      })
      return false
    }

    if (myApp.time.start_at == '') {
      wx.showToast({
        icon: 'none',
        title: '请选择服务时间',
      })
      return false
    }

    if (myApp.cleaner.id == '') {
      wx.showToast({
        icon: 'none',
        title: '请选择保洁',
      })
      return false
    }

    if (!that.data.agreement) {
      wx.showToast({
        icon: 'none',
        title: '请阅读用户协议',
      })
      return false
    }
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/setmeal/to-order?access_token=' + accessToken,
        data: {
          id: myApp.meal_id,
          product_id: myApp.product.id,
          address_id: myApp.address.id,
          start_at: myApp.time.start_at,
          end_at: myApp.time.end_at,
          hours: myApp.time.hours,
          provider_id: myApp.cleaner.id,
          leave_message: that.data.remark
        },
        success: res => {
          if(res.data.status === 1) {
            wx.hideLoading()
            wx.showModal({
              showCancel: false,
              title: '提示',
              content: '套餐下单成功',
              success: res => {
                if(res.confirm) {
                  wx.reLaunch({
                    url: '/pages/success/success',
                  })
                  wx.request({
                    url: app.globalData.hostAPI + '/v1/order/send-message?access_token=' + accessToken,
                    data: {
                      out_trade_no: res.data.data.out_trade_no
                    },
                    success: res => {
                      console.log(res)
                    }
                  })
                }
              }
            })
          } else if(res.data.status === 2) {
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: '该保洁时间段被占用，请重新选择或更换保洁',
            })
          }else {
            wx.showToast({
              icon: 'none',
              title: res.data.msg,
            })
          }
        }
      })
    })
  },

  /**
   * 备注
   */
  remakValue: function(e) {
    this.setData({
      remark: e.detail.value
    })
  },

  agreement: function (e) {
    if (this.data.agreement) {
      this.setData({
        agreement: false
      })
    } else {
      this.setData({
        agreement: true
      })
    }
  }
})